angular.module('start.common.iframe.ctrl', [])

  .controller('iframeCtrl', function($scope,$ionicLoading, $cordovaClipboard, $ionicActionSheet, $stateParams, $sce, $rootScope, $location) {
    // encodeURIComponent() -> decodeURIComponent()
    $scope.iframeType = decodeURIComponent($stateParams.type) || '';
    $scope.iframeName = decodeURIComponent($stateParams.name) || '';
    $scope.iframeUrl = decodeURIComponent($stateParams.url);
    console.log($stateParams.url);
    if ($scope.iframeUrl == null || $scope.iframeUrl == undefined) {
      $location.path("tab/home");
    }

    $scope.iframeUrl = $sce.trustAsResourceUrl($scope.iframeUrl);

    $scope.showionicActionSheet = function() {

      var buttons = [];
      if ($rootScope.config.isIOS) {
        buttons = [
          { text: '复制链接' },
        ];
      } else if ($rootScope.config.isAndroid) {
        buttons = [
          { text: '复制链接' },
          { text: '微信分享' },
        ];
      }
      // 显示操作表
      $ionicActionSheet.show({
        buttons: buttons,
        cancelText: '关闭',
        buttonClicked: function(index) {
          if (index == 0) {
            $cordovaClipboard
              .copy(''+$scope.iframeUrl)
              .then(function() {
                $ionicLoading.show({
                  template: "链接复制成功",
                  noBackdrop: true,
                  duration: 2500
                });
              }, function() {
                // error
              });
          } else if (index == 1) {
            $scope.share_wechat();
          }
          return true;
        }
      });

    };


    $scope.share_wechat = function() {
      $ionicLoading.show({
        noBackdrop: true,
        duration: 10000
      });
      document.addEventListener("deviceready", function() {
        Wechat.share({
          message: {
            title: $scope.iframeName,
            description: $scope.iframeName+' - 来自伴你同游的分享',
            thumb: "https://www.bannitongyou.com/uploads/logo-100-blank.jpg",
            mediaTagName: "",
            messageExt: "",
            messageAction: "<action>dotalist</action>",
            media: {
              type: Wechat.Type.WEBPAGE,
              webpageUrl:''+ $scope.iframeUrl
            }
          },
          scene: Wechat.Scene.SESSION // share to SESSION
        }, function() {
          $ionicLoading.hide();
        }, function(reason) {
          $ionicLoading.hide();
        });
        $ionicLoading.show({
          noBackdrop: true,
        });
      });
    };


  });
